<?php

namespace app\home\controller;

use app\common\controller\BaseController;
use app\common\model\hfztcy\LibAdminBalanceRecords;
use app\common\model\hfztcy\LibBalanceRecords;
use app\common\model\hfztcy\LibDistribute;
use app\common\model\hfztcy\LibMember;
use app\common\model\hfztcy\LibMemberDistribute;
use app\common\model\hfztcy\LibOrder;
use app\common\model\hfztcy\LibOrderRedMother;
use app\common\model\LibAdmin;
use app\common\model\LibUser;
use think\Db;

/**
 *
 * @name Test.php
 * @package hfztcy
 * @category controller
 * @link http://www.changekeji.com
 * @author rxy
 * @version 2.0
 * @copyright CHANGE INC
 * @since 2022-06-22
 */
class Test extends BaseController
{
    public function index ($code)
    {
        $adminModel = new LibAdmin();
        $userModel = new LibUser();
        $memberModel = new LibMember();
        $balanceRecords = new LibBalanceRecords();
        $adminBalanceRecords = new LibAdminBalanceRecords();
        $orderModel = new LibOrder();
        $findOrderRes = $orderModel->findOrder(['code' => $code]);
        if ($findOrderRes['errorCode'] == 0) {
            $orderRedMotherModel = new LibOrderRedMother();
            $findOrderRedMotherRes = $orderRedMotherModel->findOrder(['order_id' => $findOrderRes['data']['id']]);
            if ($findOrderRedMotherRes['errorCode'] == 0) {
                // 回退
//                db('hfztcy_commission_records')->where(['order_id' => $findOrderRes['data']['id']])->delete();
//                db('hfztcy_admin_commission_records')->where(['order_id' => $findOrderRes['data']['id']])->delete();
//                // 直推红娘
//                if ($findOrderRedMotherRes['data']['direct_user_id']) {
//                    $userModel->decreaseField(['id' => $findOrderRedMotherRes['data']['direct_user_id']],'balance',$findOrderRedMotherRes['data']['zt_commission_money']);
//                    $userModel->decreaseField(['id' => $findOrderRedMotherRes['data']['direct_user_id']],'total_balance',$findOrderRedMotherRes['data']['zt_commission_money']);
//                    $memberModel->decreaseField(['user_id' => $findOrderRedMotherRes['data']['direct_user_id']],'balance',$findOrderRedMotherRes['data']['zt_commission_money']);
//                    $memberModel->decreaseField(['user_id' => $findOrderRedMotherRes['data']['direct_user_id']],'total_balance',$findOrderRedMotherRes['data']['zt_commission_money']);
//                }
//                // 间推红娘
//                if ($findOrderRedMotherRes['data']['indirect_user_id']) {
//                    $userModel->decreaseField(['id' => $findOrderRedMotherRes['data']['indirect_user_id']],'balance',$findOrderRedMotherRes['data']['jt_commission_money']);
//                    $userModel->decreaseField(['id' => $findOrderRedMotherRes['data']['indirect_user_id']],'total_balance',$findOrderRedMotherRes['data']['jt_commission_money']);
//                    $memberModel->decreaseField(['user_id' => $findOrderRedMotherRes['data']['indirect_user_id']],'balance',$findOrderRedMotherRes['data']['jt_commission_money']);
//                    $memberModel->decreaseField(['user_id' => $findOrderRedMotherRes['data']['indirect_user_id']],'total_balance',$findOrderRedMotherRes['data']['jt_commission_money']);
//                }
//                // 市代用户
//                if ($findOrderRedMotherRes['data']['shi_admin_id']) {
//                    $adminModel->decreaseField(['id' => $findOrderRedMotherRes['data']['shi_admin_id']],'balance',$findOrderRedMotherRes['data']['shi_commission_money']);
//                    $adminModel->decreaseField(['id' => $findOrderRedMotherRes['data']['shi_admin_id']],'total_balance',$findOrderRedMotherRes['data']['shi_commission_money']);
//                }
//                // 区县代用户
//                if ($findOrderRedMotherRes['data']['xian_admin_id']) {
//                    $adminModel->decreaseField(['id' => $findOrderRedMotherRes['data']['xian_admin_id']],'balance',$findOrderRedMotherRes['data']['xian_commission_money']);
//                    $adminModel->decreaseField(['id' => $findOrderRedMotherRes['data']['xian_admin_id']],'total_balance',$findOrderRedMotherRes['data']['xian_commission_money']);
//                }
//                // 客服部用户
//                if ($findOrderRedMotherRes['data']['kf_admin_id']) {
//                    $adminModel->decreaseField(['id' => $findOrderRedMotherRes['data']['kf_admin_id']],'balance',$findOrderRedMotherRes['data']['kf_commission_money']);
//                    $adminModel->decreaseField(['id' => $findOrderRedMotherRes['data']['kf_admin_id']],'total_balance',$findOrderRedMotherRes['data']['kf_commission_money']);
//
//                    $kfbStaffListRes = $adminModel->findAllAdmin(['bid' => $findOrderRedMotherRes['data']['kf_admin_id']]);
//                    if ($kfbStaffListRes['errorCode'] == 0) {
//                        $money = $findOrderRedMotherRes['data']['kf_commission_money'];
//                        array_map(function ($staff) use ($adminModel,$money) {
//                            if (floatval($staff['rate'])) {
//                                $staff_commission_money = $staff['rate'] * $money;
//                                $staff_commission_money = (float)sprintf("%.2f", substr(sprintf("%.3f", $staff_commission_money), 0, -1));
//                                $adminModel->decreaseField(['id' => $staff['id']],'balance',$staff_commission_money);
//                                $adminModel->decreaseField(['id' => $staff['id']],'total_balance',$staff_commission_money);
//                            }
//                        },$kfbStaffListRes['data']);
//                    }
//                }
//                // 培训部用户
//                if ($findOrderRedMotherRes['data']['px_admin_id']) {
//                    $adminModel->decreaseField(['id' => $findOrderRedMotherRes['data']['px_admin_id']],'balance',$findOrderRedMotherRes['data']['px_commission_money']);
//                    $adminModel->decreaseField(['id' => $findOrderRedMotherRes['data']['px_admin_id']],'total_balance',$findOrderRedMotherRes['data']['px_commission_money']);
//
//                    $pxbStaffListRes = $adminModel->findAllAdmin(['bid' => $findOrderRedMotherRes['data']['px_admin_id']]);
//                    if ($pxbStaffListRes['errorCode'] == 0) {
//                        $money = $findOrderRedMotherRes['data']['px_commission_money'];
//                        array_map(function ($staff) use ($adminModel,$money) {
//                            if (floatval($staff['rate'])) {
//                                $staff_commission_money = $staff['rate'] * $money;
//                                $staff_commission_money = (float)sprintf("%.2f", substr(sprintf("%.3f", $staff_commission_money), 0, -1));
//                                $adminModel->decreaseField(['id' => $staff['id']],'balance',$staff_commission_money);
//                                $adminModel->decreaseField(['id' => $staff['id']],'total_balance',$staff_commission_money);
//                            }
//                        },$pxbStaffListRes['data']);
//                    }
//                }
//
//                $findUserRes = (new LibUser())->findUser(['id' => $findOrderRes['data']['user_id']]);
//                $fields = (new \app\common\model\service\LibOrder($findUserRes['data']))->updateOrderByRedMotherAuth($findOrderRes['data']['user_id']);
//                $orderRedMotherModel->updateOrder(['order_id' => $findOrderRes['data']['id']] , $fields);

                // 订单信息
                $sql = <<< SQL
SELECT a.id,a.code,a.type,a.user_id,
       b.amount,b.state,b.direct_user_id,b.indirect_user_id,b.shi_admin_id,b.xian_admin_id,b.kf_admin_id,b.px_admin_id,
       b.zt_commission_money,b.jt_commission_money,b.shi_commission_money,b.xian_commission_money,b.kf_commission_money,b.px_commission_money,b.pt_commission_money
FROM `hfztcy_order` AS a 
    INNER JOIN `hfztcy_order_red_mother` AS b 
        ON `a`.`id` = `b`.`order_id` 
WHERE `a`.`code` = '{$code}'
SQL;
                $result = Db::query($sql);
                $orderInfo = $result[0];
                dump($orderInfo);
                $res = (new LibDistribute($orderInfo))->updateUserBalance();
                dump($res);
            }
        }
    }


    public function index2 ()
    {
//        $openid = 'oeLML6xCplmALiv8jauMg4X96Wpo1234';
//        $user_id = 115;
//        (new \app\common\model\service\LibMemberDistribute($openid,$user_id))->bindUser();
        $partnerDistributeModel = new \app\common\model\hfztcy\LibMemberDistribute();
        $sql = "SELECT a.id,a.parent_id,b.user_id FROM `base_user` as a left join `hfztcy_member_distribute` as b on a.id = b.user_id WHERE b.user_id is null ORDER BY `id` DESC;";
        $result = Db::query($sql);
        $data = [];
        foreach ($result as $item) {
            $user['user_id'] = $item['id'];//扫码用户ID
            $user['one_user_id'] = 0;
            $user['two_user_id'] = 0;
            $user['pid'] = 0;
            $user['use_level'] = 0;
            $data[] = $user;
        }
        $partnerDistributeModel->insertAll($data);
    }
}